package net.wofly.cashier.dishes.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.wofly.cashier.dishes.domain.DailyCommodityNum;
import net.wofly.cashier.dishes.service.IDishesService;
import net.wofly.cashier.dishes.validator.DishesValidator;
import net.wofly.common.web.BaseController;
import net.wofly.framework.util.ResultCode;
import net.wofly.framework.util.ReturnBean;
import net.wofly.sale.commodity.domain.Commodity;

/**
 * Created by LiuJianbo on 2017/10/18.
 */
@Controller
@RequestMapping(value = "/api/dishes")
@Api(description = "菜品管理接口")
public class DishesController extends BaseController {

    @Autowired
    IDishesService iDishesService;

    /**
     * 获取所有菜品，不分页。
     * <ul>
     * <li>status=1 所有有效菜品</li>
     * <li>status=0 所有无效菜品</li>
     * <li>status=-1 所有菜品</li>
     * <li>commodityTypeID=-1时，取所有类型的菜品</li>
     * </ul>
     *
     * @param status
     * @param commodityTypeID
     */
    @ApiOperation(value = "获取所有菜品")
    @ApiImplicitParams({@ApiImplicitParam(name = "status", value = "菜品是否有效的状态（1：有效；0:无效；-1：所有菜品）,默认查询有效状态",
                                          allowableValues = "1,0,-1", required = true, dataType = "int", paramType = "form"),
                               @ApiImplicitParam(name = "commodityTypeID", value = "菜品ID", required = true, dataType = "String", paramType = "form")})
    @RequestMapping(value = "/getAllDishes", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<List<Commodity>> getAllDishes(Integer status, String commodityTypeID) {
        ReturnBean<List<Commodity>> returnBean;
        String errorMessage = DishesValidator.validateGetAllDishesParams(status, commodityTypeID);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<List<Commodity>>(ResultCode.ERROR_DATA, errorMessage);
        } else {
            returnBean = iDishesService.getAllDishes(status, commodityTypeID);
        }
        return returnBean;
    }

    /**
     * 获取所有菜品，同时返回菜品可卖数量
     * <ul>
     * <li>commodityTypeID=-1时，取所有类型的菜品</li>
     * <li>只取有效状态的菜品</li>
     * <li></li>
     * </ul>
     *
     * @param commodityTypeID
     */
    @ApiOperation(value = "获取所有菜品，同时返回菜品可卖数量")
    @ApiImplicitParams({@ApiImplicitParam(name = "commodityTypeID", value = "菜品ID", required = true, dataType = "String", paramType = "form")})
    @RequestMapping(value = "/getAllDishesByNum", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<List<Commodity>> getAllDishesByNum(String commodityTypeID) {
        ReturnBean<List<Commodity>> returnBean;
        String errorMessage = DishesValidator.validateCommodityID(commodityTypeID);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<List<Commodity>>(ResultCode.ERROR_DATA, errorMessage);
        } else {
            returnBean = iDishesService.getAllDishesByNum(commodityTypeID);
        }
        return returnBean;
    }

    /**
     * 获取菜品指定日的销售数量
     *
     * @param commodityID
     * @param currDate
     */
    @ApiOperation(value = "获取菜品指定日的销售数量")
    @ApiImplicitParams({@ApiImplicitParam(name = "commodityID", value = "菜品ID", required = true, dataType = "String", paramType = "form"),
                               @ApiImplicitParam(name = "currDate", value = "指定日期0点的毫秒时间戳", required = true, dataType = "Long", paramType = "form")})
    @RequestMapping(value = "/getValidDishesNum", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<DailyCommodityNum> getValidDishesNum(String commodityID, Long currDate) {
        ReturnBean<DailyCommodityNum> returnBean;
        String errorMessage = DishesValidator.validateGetValidDishesNumParams(commodityID, currDate);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<DailyCommodityNum>(ResultCode.ERROR_DATA, errorMessage);
        } else {
            returnBean = iDishesService.getValidDishesNum(commodityID, currDate);
        }
        return returnBean;
    }

    /**
     * 设置菜品日最大销售量
     *
     * @param dailyCommodityNum
     */
    @ApiOperation(value = "设置菜品日最大销售量")
    @ApiImplicitParams({@ApiImplicitParam(name = "dailyCommodityNum", value = "每日菜品数量实体", required = true, dataType =
            "DailyCommodityNum")})
    @RequestMapping(value = "/setDailyDishesNum", method = {RequestMethod.POST})
    @ResponseBody
    public ReturnBean<DailyCommodityNum> setDailyDishesNum(@RequestBody DailyCommodityNum dailyCommodityNum) {
        ReturnBean<DailyCommodityNum> returnBean;
        String errorMessage = DishesValidator.validateSetDailyDishesNumParams(dailyCommodityNum);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<DailyCommodityNum>(ResultCode.ERROR_DATA, errorMessage);
        } else {
            returnBean = iDishesService.setDailyDishesNum(dailyCommodityNum);
        }
        return returnBean;
    }
}
